<?php
namespace app\index\controller;
// use app\index\
class Service extends Base
{
    public function index()
    { 
        $id=request()->param('id');       
        $viewPage=$this->getIndexPage($id);  
        $service=db("cms_service");
        $channel=db("channel");
                
        $pid=$channel->where("id=".$id)->value("pid");
        if($pid){        
            $pcid=$id;
            $gcid=$pid;
            $where['cid']=$id;  
            $pnavName=$channel->where("id",$pcid)->value("name");                       
        }else{
            $gcid=$id;            
            $childId=$channel->where("status=1 and pid=".$id)->column("id");
            // print_r($childId);exit;
            $where['cid']=array("in",$childId);
        }
        $where['status']=1;   
           
        $gnavName=$channel->where("id=".$gcid)->value("name");

        // print_r(($gcid));exit;
        $prochannel=$channel->where("status= 1 and  pid=".$gcid)->order("sort asc , id DESC")->select();
        
        // 分页
        $p=request()->param('p'); 
        if(!$p){
            $p=1;
        }
        $psize=12;
        $count=$service->where($where)->count();
        $tolp=ceil($count/$psize);
        $stp=($p-1)*$psize;
        $mess=$service->where($where)->order("sort asc , id DESC")->limit($stp,$psize)->select();
        for ($i=0; $i < count($mess); $i++) { 
            $mess[$i]['cname']=$channel->where("id=".$mess[$i]['cid'])->value("name");
            if($mess[$i]['thumb']){
                $mess[$i]['annexes']=db("filex")->where("id=".$mess[$i]['thumb'])->value("thumb");  
            }
        }
        $cate=$this->get_product_cate($gcid);
        
        // 分页渲染
        $this->assign("p",$p);
        $this->assign("tolp",$tolp);
        $this->assign("count",$count);

        // SEO META关键字配置
        $meta=$this->meta;
        $meta['title']=$pnavName.' '.$gnavName.' '.$meta['title'];

        $meta['keywords']=$pnavName.$gnavName.$meta['keywords'];

        $meta['descript']=$pnavName.$gnavName.$meta['descript'];

        $this->assign("meta",$meta);
        $modelx=$channel->where("id",$gcid)->value("modelx");
        $modelxMark=db("modelx")->where("id=".$modelx)->value("mark");
        $this->assign("modelxMark",$modelxMark);
        $banner=$this->get_banner($gcid);
        $this->assign("banner",$banner);
        $this->assign("prochannel",$prochannel);
        $this->assign("cate",$cate);
        $this->assign("pcid",$pcid);
        $this->assign("pnavName",$pnavName);  
        $this->assign("lchannel",$lchannel);
        $this->assign("gcid",$gcid);        
        $this->assign("id",$id);        
        $this->assign("gnavName",$gnavName);
        
        $this->assign("mess",$mess);
        return view($viewPage);
    }
    public function detail()
    {   
        $id=request()->param('id');
        $service=db("cms_service");
        $channel=db("channel");
        
        // $cid=$product->where("id=".$id)->value("cid");
        $pcid=$service->where("id=".$id)->value("cid");
        $viewPage=$this->getDetailPage($pcid);
        $gcid=$channel->where("id=".$pcid)->value("pid");
        // $where['status']=1;
        $where['id']=$id;
        $mess=$service->where($where)->find();
        if($mess['images']){
            $photos=explode(",",$mess['images']);
            for ($i=0; $i < count($photos); $i++) { 
                $mess['photos'][]=db("filex")->where("id=".$photos[$i])->value("thumb");
            }
        }        
        $fname=$channel->where("id",$gcid)->value("name");
        $allChannel=$this->get_channel();
        foreach ($allChannel as $v) {
            if($v['id'] == $gcid){
                $lchannel=$v['child'];
            }
        }
        // print_r($mess);exit;
        $gnavName=$channel->where("id=".$gcid)->value("name");

        $pnavName=$channel->where("id=".$pcid)->value("name");
        // $cnavName=$channel->where("id=".$cid)->value("name");
        // SEO META关键字配置
        $meta=$this->meta;
        $meta['title']=$mess['title'].''.$pnavName.''.$gnavName.''.$meta['title'];
        if($mess['keywords']){
            $meta['keywords']=$mess['keywords'];
        }else{
            $meta['keywords']=$mess['title'].$pnavName.$gnavName.$meta['keywords'];
        }
        if($mess['descript']){
            $meta['descript']=$mess['descript'];
        }else{
            $meta['descript']=$mess['title'].$pnavName.$gnavName.$meta['descript'];
        }
        $this->assign("meta",$meta);
        $modelx=$channel->where("id",$gcid)->value("modelx");
        $modelxMark=db("modelx")->where("id=".$modelx)->value("mark");
        $this->assign("modelxMark",$modelxMark);
        $banner=$this->get_banner($gcid);
        $this->assign("banner",$banner);
        $this->assign("pnavName",$pnavName);
        // $this->assign("cnavName",$cnavName);
        $this->assign("gnavName",$gnavName);
        $this->assign("lchannel",$lchannel);
        $this->assign("pcid",$pcid);
        // $this->assign("cid",$cid);
        $this->assign("gcid",$gcid);
        $this->assign("fname",$fname);
        $this->assign("mess",$mess);
        return view($viewPage);
    }
}
